﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Web.UI.HtmlControls;
public partial class Main_Master_Project : System.Web.UI.Page
{
    //For the region of the Page load
    #region
    Datautility du = new Datautility();
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["View"] != null)
            {
                if (Request.QueryString["View"].ToString() == "True")
                {
                    pnlview.Visible = true;
                    pnlData.Visible = false;
                    ViewData();
                }
            }
            else
            {
                pnlData.Visible = true;
                pnlview.Visible = false;                
            }
        }
    }
    #endregion
    //for the Save data
    #region
    protected void btnSaveproject_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtproject.Text != "")
            {
                string sqlstr = "insert into MAS_PROJECT (projectname) Values ('" + txtproject.Text + "')";
                int i = du.executesql(sqlstr);
                if (i > 0)
                {
                    lblMessage.Text = "Project Inserted Successfully";
                    lblMessage.ForeColor = System.Drawing.Color.Green;
                    lblMessage.Attributes.Add("style", "display:block");  
                    txtproject.Text = "";
                    //getData();
                }
                else
                {
                    lblMessage.Text = "Error While Project Inserted ...";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                    lblMessage.Attributes.Add("style", "display:block");  
                }
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Attributes.Add("style", "display:block");
        }
    }
    #endregion
    //For the Bind of the ProjectDetails
    #region
    protected void ViewData()
    {
        string sql = "select * from Mas_Project";
        dt = du.getdata(sql);
        if (dt.Rows.Count > 0)
        {
            DataView dv = dt.DefaultView;
            if (this.ViewState["SortExpression"] != null)
            {
                dv.Sort = string.Format("{0} {1}", ViewState["SortExpression"].ToString(), this.ViewState["SortOrder"].ToString());
            }
            grdprojectview.DataSource = dt;
            grdprojectview.DataBind();
        }
    }
    #endregion    
    //For the Gridview of the Bound.
    #region
    protected void grdprojectview_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            string id = grdprojectview.DataKeys[e.RowIndex].Value.ToString();
            TextBox txtprojectname = (TextBox)grdprojectview.Rows[e.RowIndex].FindControl("txtprojectname");
            string sql = "update MAS_PROJECT set projectname='" + txtprojectname.Text + "' where projectid=" + id;
            int j= du.executesql(sql);
            grdprojectview.EditIndex = -1;            
            ViewData();
            if (j > 0)
            {
                lblMessage.Text = "Project Updated SuccessFully";
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Visible = true;
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Error While Updating ....";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Visible = true;
        }
    }
    protected void grdprojectview_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdprojectview.EditIndex = -1;
        ViewData();
    }
    protected void grdprojectview_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdprojectview.EditIndex = e.NewEditIndex;
        ViewData();
    }
    protected void grdprojectview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdprojectview.PageIndex = e.NewPageIndex;
        ViewData();
    }
    protected void grdprojectview_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Sort"))
        {
            if (ViewState["SortExpression"] != null)
            {
                if (this.ViewState["SortExpression"].ToString() == e.CommandArgument.ToString())
                {
                    if (ViewState["SortOrder"].ToString() == "ASC")
                        ViewState["SortOrder"] = "DESC";
                    else
                        ViewState["SortOrder"] = "ASC";
                }
                else
                {
                    ViewState["SortOrder"] = "ASC";
                    ViewState["SortExpression"] = e.CommandArgument.ToString();
                }
            }
            else
            {
                ViewState["SortExpression"] = e.CommandArgument.ToString();
                ViewState["SortOrder"] = "ASC";
            }
            ViewData();
        }
    }
    #endregion
    //for the Export on the Different Format.
    #region
    protected void imgxl_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=Project_Report.xls");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HtmlForm hf = new HtmlForm();
            grdprojectview.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            grdprojectview.Columns[grdprojectview.Columns.Count - 1].Visible = false;
            grdprojectview.AllowPaging = false;
            grdprojectview.HeaderRow.Style.Add("width", "15%");
            grdprojectview.HeaderRow.Style.Add("font-size", "10px");
            grdprojectview.Style.Add("text-decoration", "none");
            grdprojectview.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdprojectview.Style.Add("font-size", "8px");
            hf.Controls.Add(grdprojectview);
            hf.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {

        }
    }
    protected void imgword_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/vnd.word";
            Response.AddHeader("content-disposition", "attachment;filename=Project_Report.doc");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            HtmlForm hf = new HtmlForm();
            grdprojectview.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            grdprojectview.Columns[grdprojectview.Columns.Count - 1].Visible = false;
            grdprojectview.AllowPaging = false;
            grdprojectview.HeaderRow.Style.Add("width", "15%");
            grdprojectview.HeaderRow.Style.Add("font-size", "10px");
            grdprojectview.Style.Add("text-decoration", "none");
            grdprojectview.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdprojectview.Style.Add("font-size", "8px");
            hf.Controls.Add(grdprojectview);
            hf.RenderControl(htw);

            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {

        }
    }
    protected void imgpdf_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Project_Report.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            HtmlForm hf = new HtmlForm();
            grdprojectview.Parent.Controls.Add(hf);
            hf.Attributes["runat"] = "server";

            grdprojectview.Columns[grdprojectview.Columns.Count - 1].Visible = false;
            grdprojectview.AllowPaging = false;
            grdprojectview.HeaderRow.Style.Add("width", "15%");
            grdprojectview.HeaderRow.Style.Add("font-size", "10px");
            grdprojectview.Style.Add("text-decoration", "none");
            grdprojectview.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdprojectview.Style.Add("font-size", "8px");
            //grdview.DataBind();

            hf.Controls.Add(grdprojectview);
            hf.RenderControl(htw);

            //grdview.RenderControl(htw);           
            StringReader sr = new StringReader(sw.ToString());
            Document pdf = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdf);
            PdfWriter.GetInstance(pdf, Response.OutputStream);
            pdf.Open();
            htmlparser.Parse(sr);
            pdf.Close();
            Response.Write(pdf);
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        base.VerifyRenderingInServerForm(control);
    }
    #endregion
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            ViewData();
            dt.DefaultView.RowFilter = "Projectname like '%" + txtSearchProject.Text + "%'";
            if (dt.Rows.Count > 0)
            {
                grdprojectview.DataSource = dt;
                grdprojectview.DataBind();
            }
            else
            {
                lblMsgView.Text = "No Record Found !";
                lblMsgView.ForeColor = System.Drawing.Color.Green;
                lblMsgView.Attributes.Add("style", "display:block"); 
            }
        }
        catch (Exception Ex)
        {
        }
    }
}